package com.common.app.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import com.common.app.R
import com.common.app.data.bean.CalendarDay
import com.common.app.databinding.DialogCalendarSelectLayoutBinding
import com.common.base.app.extras.postDelay
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.showToast
import com.common.base.utils.ViewUtils
import java.util.Calendar

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/07/17
 *
 * 描述：日期选择弹框
 *
 * 修订历史：
 *
 */
class SelectCalendarDialog(context: Context) :
    Dialog(context), View.OnClickListener {

    private val mContext: Context

    private var binding: DialogCalendarSelectLayoutBinding? = null

    private var listener:OnSelectCalendarDialogListener?=null

    private var curDay: CalendarDay?=null

    private val hasEventDayList = mutableListOf<String>()

    private var showGreyPoint: Boolean = true

    private var limitYears = 2 // 可以往前切换3年

    init {
        setDialogTheme()
        setCanceledOnTouchOutside(true)
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // dialog 初始化的时候调用(PS:保证Dialog弹出后状态栏文字变色)
        window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        binding = DialogCalendarSelectLayoutBinding.inflate(LayoutInflater.from(context))
        binding?.root?.let { setContentView(it) }
        binding?.apply {
            // 设置年限限制（可以往前切换3年）
            calendarPicker.setLimitYears(limitYears)

            // 设置日期选择监听
            calendarPicker.setOnDateSelectedListener { day ->
                curDay = day
                listener?.onSelectCalendarDialogClick(day)
                if(isShowing) dismiss()
            }

        }
        binding?.tvToTheDay?.setOnClickListener(onClick = this)
    }

    override fun show() {
        super.show()
        //设置弹框的高为屏幕的一半宽是屏幕的宽
        val mDialogLp = this.window?.attributes

        //设置宽度
        mDialogLp?.width = ViewGroup.LayoutParams.MATCH_PARENT
//        mDialogLp?.height = (ViewUtils.getScreenHeight() * 0.4).toInt()
        mDialogLp?.height = ViewGroup.LayoutParams.WRAP_CONTENT

        mDialogLp?.gravity = Gravity.BOTTOM //设置dialog 在布局中的位置
        this.window?.attributes = mDialogLp
        window?.setWindowAnimations(R.style.bottomShowStyle) //添加动画:

        binding?.calendarPicker?.setShowGreyPoint(showGreyPoint)

        binding?.calendarPicker?.setHasEventDayList(hasEventDayList)

        curDay?.let {
            // 设置选中日期并跳转到对应的日历页
            binding?.calendarPicker?.setSelectedDayByCalendarDay(it)
        }

    }


    override fun onClick(v: View) {
        when (v) {
            binding?.tvToTheDay -> {
                binding?.calendarPicker?.goToToday()
            }
        }
    }

    fun initData() {

    }

    /**
     * set dialog theme(设置对话框主题)
     */
    private fun setDialogTheme() {
        requestWindowFeature(Window.FEATURE_NO_TITLE) // android:windowNoTitle
        window?.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) // android:backgroundDimEnabled默认是true的
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) // android:windowBackground
    }

    fun setListener(mListener:OnSelectCalendarDialogListener?){
        listener = mListener
    }

    fun setCurDay(mDay: Calendar?=null){
        if (mDay==null) return
        curDay = binding?.calendarPicker?.getSelectedDayByCalendar(mDay)
    }

    fun setCurDay(mDay: CalendarDay?=null){
        if (mDay==null) return
        curDay = mDay
    }


    fun setLimitYears(mLimitYears: Int){
        limitYears = mLimitYears
    }

    /**
     * 设置有事件的日期列表
     */
    fun setHasEventDayList(eventDays: List<String>) {
        hasEventDayList.clear()
        hasEventDayList.addAll(eventDays)
    }

    fun setShowGreyPoint(show:Boolean){
        showGreyPoint = show
    }

    /**
     * 回调接口
     */
    interface OnSelectCalendarDialogListener {
        fun onSelectCalendarDialogClick(calendarDay: CalendarDay)
    }
}
